<template>
	<view class="add_picture">
		<Input style="border: none;" type="text" readonly="readonly" id="password-keyboard10" 
			placeholder="请输入开户密码" class="default custom-class1" maxlength="50" />
	</view>
</template>
<script module="three" lang="renderjs">
	const app = getApp();
	if($("#testkbid")[0] == undefined){
		console.log('第一次绘制')
		getRandom()
	}else{
		console.log('已经绘制过了，重新绘制')
		getRandom()
	}

	
	console.log('密码控件组件',window)
	let random = {
		license: '',
		map_arr: '',
		random_key: '',
		random_value: '',
		rsa_public_content: ''
	}
	let microDone
	let pwd
	
	function initMicroDone(props) {
		const svgPath = 'https://mpay-static.oss-cn-hangzhou.aliyuncs.com/html/lib/microdone/svg' // svg图片的地址
		const {
			guardName,
			random
		} = props
		console.log('1111', window.keyBoard)
		if (typeof window.kb === 'undefined') {
			// 键盘构造
			window.kb = new window.keyBoard({
				chaosMode: 0, // 混乱模式,0:无混乱;1:打开时乱一次;2:每输入一个字符乱一次,默认值0
				pressStatus: 1, // 按键状态,0:按下、抬起按键无变化;1:按下、抬起按键的颜色变化,默认值0
				kbType: 0, // 键盘类型,0:全键盘;1:纯数字键盘,默认值0
				svg: svgPath,
				hasMap: '1', // 是否调用mapping值：当为1时调用，当为非1时，不调用
				license: random.license
			})
		}
		try {
			if ($("#testkbid")[0] === undefined) {
				window.kb.generate()
			}
		} catch (e) {
			console.log(e)
		}
		window.passGuard.ib = random.map_arr
		console.log(window[guardName]);
		if (typeof window[guardName] === 'undefined') {
			// 密码卫士构造
			window[guardName] = new window.passGuard({
				jump: 1,
				fixed: 'H5fixed', // 当fixed接口添加时，代表该输入框在fixed覆盖层上且覆盖层的ID为接口的值(在demo中覆盖层ID为"H5fixed")
				maxLength: 12, // 最大输入长度
				regExp1: '[\\S\\s]', // 输入过程限制的正则
				regExp2: '[\\S\\s]{6,12}',
				displayMode: 0, // 显示形式,0:星号;1:明文,默认值0
				callBack: () => {}, // 成功回调
				errorCallBack: () => {}, // 失败回调
				focus: () => {}, // H5键盘获取焦点回调
				blur: () => {
					a()
					// console.log('失去焦点', guardName)
				}, // H5键盘失去焦点回调
				add: () => {},
				del: () => {},
				rsaPublicKey: random.rsa_public_content,
			})
			console.log(window[guardName]);
		}
		return window[guardName]
	}
	
	function getValue(guard) {
		return new Promise((resolve, reject) => {
			if (guard.getLength() === 0) {
				reject('EMPTY')
			}
			if (guard.getValid() === 1) {
				return reject('FORMAT_ERROR')
			}
			// ib is cleared after passGuardKeyboard.getOutput(), thus needs to be reassigned
			const password = guard.getOutput()
			// guard.clearpwd()
			window.passGuard.ib = random.map_arr
			resolve(password)
		})
	}
	
	function a() {
		getValue(microDone).then(password => {
			pwd = password
			 window['pwdcontrol'] = password
			 console.log('pwd', window.pwdcontrol)
			console.log('pwd', pwd)
			// console.log("密码1的hash是，用于比对密码相等", window.passGuardKeyboard.getHash())
			console.log('获取密码成功，密码是', password)
		}).catch(code => {
			// code EMPTY-input值为空 FORMAT_ERROR-密码格式校验不正确
			console.log('获取密码失败', code)
		})
	}
	function getRandom() {
				let _this = this
				app._post_form('user/getRandom', {}, function(result) {
					if (1 == result.code) {
						// console.log('我是密码控件')
						window['random'] = result.data
						random.map_arr = result.data.map_arr,
						random.license = result.data.license,
						random.random_key = result.data.random_key
						random.random_value = result.data.random_value
						random.rsa_public_content = result.data.rsa_public_content
						console.log(random)
						microDone = initMicroDone({
							inputId: 'password-keyboard10',
							guardName: 'passGuardKeyboard10',
							random
						})
						console.log(document.getElementById("password-keyboard10"))
						microDone.generate('password-keyboard10', window.kb, 'passGuardKeyboard10', 0) // 密码卫士初始化
						microDone.setRandKey(random.random_value) // 设置随机因子
			
						// 初始化键盘
						window.kb.generate();
						// uni.stopPullDownRefresh();
			
					}
				}, null, function() {
				});
			}
			window['getRandom'] = () =>{
				getRandom()
			}
	export default {
		 props: {
		    control: {
		      type: Boolean,
		      default: null,
		    },
		  },
		data() {
			return {
			}
		},
		onLoad() {
			console.log('111',this.control)
		},
		onShow() {
			console.log('密码控件onshow')
		},
		onReady() {
		},
		methods: {
			// 获取随机因子
			
		},
		watch:{
			control(val, oldVal){
				console.log('密码控件变化')
			}
		},
	}
</script>
<style>
</style>
